package com.firstcare.android_dylibrary.bean;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.firstcare.android_dylibrary.DYApplication;
import com.firstcare.android_dylibrary.bean.base.BaseModelProxy;
import com.firstcare.android_dylibrary.network.URLSet;
import com.firstcare.android_dylibrary.network.request.ModelListRequest;
import com.rdxer.xxlibrary.HTTPUtils.listener.ErrorListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.FailedListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.OKListener;

import java.util.List;

/**
 * Created by LXF on 16/6/12.
 */

public class HomeProxy extends BaseModelProxy<Home> {

    public static void getHomeList(final OKListener<List<HomeProxy>> okListener, FailedListener failedListener, final ErrorListener errorListener) {
        ModelListRequest<Home> request = new ModelListRequest<Home>(URLSet.getHomeList, null, new OKListener<List<Home>>() {
            @Override
            public void onOK(List<Home> res, JSONObject response) {
                List<HomeProxy> hp = HomeProxy.generateModelProxyList(res, HomeProxy.class);
                okListener.onOK(hp, response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.getJSONObject("data").getJSONArray("homeList");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }


    /**
     *  使用缓存
     */
    public static void getHomeProxyList(final OKListener<List<HomeProxy>> okListener, FailedListener failedListener, ErrorListener errorListener) {
        ModelListRequest<HomeProxy> request = new ModelListRequest<HomeProxy>(URLSet.getHomeList, null, new OKListener<List<HomeProxy>>() {
            /// 有网加载数据成功,存入缓存.
            @Override
            public void onOK(List<HomeProxy> res, JSONObject response) {
                /// 保存带数据库
                // 如果需要清空数据库
//                deleteFromDB(Home.class);
//                saveToDB(res);
                okListener.onOK(res, response);
            }
        },
                // 如果在获取失败的时候也使用缓存可以重写并且调用 okLxxxx 的方法
                failedListener
                , new ErrorListener() {
            /// 没网 走的是网络错误的方法
            @Override
            public void onErrorResponse(VolleyError error) {
                List<HomeProxy> homeProxyList = getDataFromDB(Home.class,HomeProxy.class);
                okListener.onOK(homeProxyList,null);
            }
        }) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.getJSONObject("data").getJSONArray("homeList");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }

    /**
     *  不使用缓存
     */
    public static void getHomeProxyListNoCache(final OKListener<List<HomeProxy>> okListener, FailedListener failedListener, ErrorListener errorListener) {
        ModelListRequest<HomeProxy> request = new ModelListRequest<HomeProxy>(URLSet.getHomeList, null, okListener, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.getJSONObject("data").getJSONArray("homeList");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
}
